<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="图书副本ID" prop="bookDetailId">
      <el-input v-model="dataForm.bookDetailId" placeholder="图书副本ID"></el-input>
    </el-form-item>
    <el-form-item label="读书证ID" prop="readerCardId">
      <el-input v-model="dataForm.readerCardId" placeholder="读书证ID"></el-input>
    </el-form-item>
    <el-form-item label="借阅状态 1-待归还 2-已逾期 3-已归还" prop="status">
      <el-input v-model="dataForm.status" placeholder="借阅状态 1-待归还 2-已逾期 3-已归还"></el-input>
    </el-form-item>
    <el-form-item label="借阅时间" prop="borrowDate">
      <el-input v-model="dataForm.borrowDate" placeholder="借阅时间"></el-input>
    </el-form-item>
    <el-form-item label="到期时间" prop="expireDate">
      <el-input v-model="dataForm.expireDate" placeholder="到期时间"></el-input>
    </el-form-item>
    <el-form-item label="归还时间" prop="backDate">
      <el-input v-model="dataForm.backDate" placeholder="归还时间"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="note">
      <el-input v-model="dataForm.note" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          bookDetailId: '',
          readerCardId: '',
          status: '',
          borrowDate: '',
          expireDate: '',
          backDate: '',
          note: ''
        },
        dataRule: {
          bookDetailId: [
            { required: true, message: '图书副本ID不能为空', trigger: 'blur' }
          ],
          readerCardId: [
            { required: true, message: '读书证ID不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '借阅状态 1-待归还 2-已逾期 3-已归还不能为空', trigger: 'blur' }
          ],
          borrowDate: [
            { required: true, message: '借阅时间不能为空', trigger: 'blur' }
          ],
          expireDate: [
            { required: true, message: '到期时间不能为空', trigger: 'blur' }
          ],
          backDate: [
            { required: true, message: '归还时间不能为空', trigger: 'blur' }
          ],
          note: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/tborrow/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.bookDetailId = data.tBorrow.bookDetailId
                this.dataForm.readerCardId = data.tBorrow.readerCardId
                this.dataForm.status = data.tBorrow.status
                this.dataForm.borrowDate = data.tBorrow.borrowDate
                this.dataForm.expireDate = data.tBorrow.expireDate
                this.dataForm.backDate = data.tBorrow.backDate
                this.dataForm.note = data.tBorrow.note
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/tborrow/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'bookDetailId': this.dataForm.bookDetailId,
                'readerCardId': this.dataForm.readerCardId,
                'status': this.dataForm.status,
                'borrowDate': this.dataForm.borrowDate,
                'expireDate': this.dataForm.expireDate,
                'backDate': this.dataForm.backDate,
                'note': this.dataForm.note
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
